<template>
<div  id="building">
  <div class="app-container common-list-page">
    <el-form
      :model="resetForm"
      :rules="resetFormRules"
      ref="resetForm"
      status-icon
      label-width="100px"
    >
  
      <el-form-item label="手机号" prop="phone" style="width: 340px">
       <el-input type="text" maxlength="11" suffix-icon="el-icon-mobile-phone" placeholder="手机号" v-model="form.phone" />
       </el-form-item>
      <el-form-item label="" prop="code" style="width: 340px">                      
       <el-input type="text" maxlength="6" suffix-icon="el-icon-message" placeholder="验证码" v-model="form.code" clearable />
      <el-button class="btn-orange" :disabled="disabled" @click="getCode()">{{valiBtn}}</el-button>                        
      </el-form-item>
      <!-- <el-form-item label="旧密码：" prop="password" style="width: 340px">
        <el-input type="password" v-model="resetForm.password" auto-complete="off" ></el-input>
      </el-form-item> -->
      <el-form-item label="新密码：" prop="password" style="width: 340px">
        <el-input type="password" v-model="resetForm.password" auto-complete="off" ></el-input>
      </el-form-item>
      <el-form-item label="确认密码：" prop="newpassword1" style="width: 340px">
        <el-input type="password" v-model="resetForm.newpassword1" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item style="width: 340px">
        <el-button type="primary" @click="confirmedit()">确认修改</el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>

<script>
import picture from '@/assets/img/lut.jpg'

export default {
  data() {
    var checkPhone = (rule, value, cb) => {
      console.log("ndkv"+value)
              const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
            if (regMobile.test(this.form.phone)) {
                // 合法的手机号码
                return cb() 
            }
            cb(new Error('手机号码格式不正确'))
              
               };
    var validatePass = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入新密码"));
      } else if (value.toString().length < 6 || value.toString().length > 18) {
        callback(new Error("密码长度为6-18位"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
       console.log("ndkv"+value)
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.resetForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      styles:{
      backgroundRepeat: "no-repeat",
        backgroundSize: "100% 100%",
        width:'1000px',
  
     backgroundImage:`url(${picture})`
      },
       form: {
              phone: '',
              code: ''
           },
           autoLogin:false,
          
            dialogVisible: false,     // 对话框显示和隐藏
            valiBtn:'获取验证码',
            disabled:false,
      resetForm: {
      //传给后台所需要的参数
        newpassword1: "",
        password: "",
      },
      resetFormRules: {
         phone: [{ required: true,validator: checkPhone, trigger: 'blur' } ],
        
          
        password: [
            { required: true, validator: validatePass, trigger: 'blur' }
        ],
        newpassword1: [
          { required: true, validator: validatePass2, trigger: "blur" }
        ]
      },
      rules: {
       code: [
            { required: true, message: '请选择活动区域', trigger: 'blur' }
          ],
      },

    };
  },
  methods: {
    // 确认修改
    confirmedit(){
      this.$http({
      url: this.$http.adornUrl('/sys/user/edit'),
      method: 'get',
      params: this.$http.adornParams({
        'phone' : this.form.phone,
        'password': this.resetForm.password,
        'code':this.form.code
      })
      }).then(({data}) => {
        if(data.code!=0){
          this.$alert(data.msg, '密码重置', {
          confirmButtonText: '确定',
          callback: action => {
            this.$message({
              type: 'info',
              message: `action: ${ action }`
            });
          }
        });
          

        }else{
          this.$router.push('/login')
        }
        
      })

    },
    // 获取验证码
    getCode(){
      this.$http({
      url: this.$http.adornUrl('/sys/user/send'),
      method: 'get',
      params: this.$http.adornParams({
        'phone' : this.form.phone
      })
      }).then(({data}) => {
        console.log("cnklds"+data)
        if (data.code != 0) {
           this.$alert('用户不存在，请输入正确的电话号码！！', '密码重置', {
          confirmButtonText: '确定',
          callback: action => {
            this.$message({
              type: 'info',
              message: `action: ${ action }`
            });
          }
        });
          
          
        }
        
      })

    },
    toAmend() {
      this.$refs.resetForm.validate(valid => {
        if (valid) {
        //这里的api.materialQuery.toAmend是调用前期我们统一的api接口url路径，不作参考 ，只要把后台需要的字段正常传进去即可 
          api.materialQuery.toAmend(this.resetForm)
            .then(res => {
              if(res.code === 2){
                this.$message({
                    message: res.msg,
                    type: "error",
                    duration: "2000"
                  });
                return false;
              }
              if (res.code === 0) {
                this.$message.success("修改成功,3秒后跳转到登录页！");
                setTimeout(() => {
                   this.logout();//调用跳转到登陆页的方法
                }, 3000);
              }
              ic
            })
            .catch(() => {});
        }
      });
    },
    //这是修改成功后重新返回登陆页的方法，看个人需要自行调整
    async logout() {
      await this.$store.dispatch("user/logout");
      this.$router.push(`/login`);
    }
  },
};
</script>

<style lang="scss">
.el-form {
  width: 60%;
  margin: 50px auto 0;
  text-align: center;
  button {
    margin: 20px 0 0;
  }
}
#building{
background:url("../../assets/img/lut.jpg");
width:100%;			//大小设置为100%
height:100%;			//大小设置为100%
position:fixed;
background-size:100% 100%;}
</style>
